function choose_good(){
	var box=document.getElementById("choose_area");
	box.src="sale/chooseGood";
	showDiv('choose_frame','event',550,400);
}

function choose_done(name,id,amount,price){
	var box=document.getElementById("good_list");
	var dis=document.getElementById("good_list_display");
	var total=document.getElementById("total_price");
	var list=box.value;
	var dis_str=dis.innerHTML;
	var array=list.split(";");
	var index=array.length-1;
	dis_str+="["+id+"]&nbsp;&nbsp;&nbsp;&nbsp;"+name+"&nbsp;×&nbsp;"+amount+"&nbsp;=&nbsp;"+amount*price+
				"&nbsp;&nbsp;<a href='javascript:delete_goods("+index+")'>[删除]</a>"+
				"&nbsp;&nbsp;<a href='javascript:modify_price("+index+")'>[修改单价]</a><br>";
	list+=id+"#"+amount+"#"+price+";";
	box.value=list;
	dis.innerHTML=dis_str;
	total.innerHTML=total.innerHTML*1+amount*price;
}


function choose_return(){
	var box=document.getElementById("choose_area");
	box.src="sale/chooseGood";
	showDiv('choose_frame','event',550,400);
}

function delete_goods(index){
	var box=document.getElementById("good_list");
	var dis=document.getElementById("good_list_display");
	var total=document.getElementById("total_price");
	var list=box.value;
	var dis_str=dis.innerHTML;
	var array=list.split(";");
	list="";
	for(i=0;i<array.length-1;i++){
		if(i!=index)
			list+=array[i]+";";
	}
	box.value=list;
	var goods=array[index];
	array=goods.split("#");
	total.innerHTML=total.innerHTML*1-array[1]*array[2];
	array=dis_str.split("<br>");
	dis_str="";
	var delete_after=false;
	for(i=0;i<array.length-1;i++){
		if(delete_after){
			array[i]=array[i].replace("delete_goods("+i+")","delete_goods("+(i-1)+")");
			array[i]=array[i].replace("modify_price("+i+")","modify_price("+(i-1)+")");
		}
		if(i!=index)
			dis_str+=array[i]+"<br>";
		else
			delete_after=true;
	}
	dis.innerHTML=dis_str;
}

var mp_index=0;
function modify_price(index){
	mp_index=index;
	var box=document.getElementById("good_list");
	var list=box.value;
	var array=list.split(";");
	var goods=array[index];
	array=goods.split("#");
	document.getElementById("modify_price_input").value=array[2];
	showDiv('modify_price_div','event',300,300);
}

function modify_price_done(){
	closeDiv('modify_price_div');
	var price=document.getElementById("modify_price_input").value;
	var regNum =/^\d*$/;
	if(!regNum.test(price)){
		alert('数量必须为数字');
		return;
	}
	
	var box=document.getElementById("good_list");
	var dis=document.getElementById("good_list_display");
	var total=document.getElementById("total_price");
	var list=box.value;
	var dis_str=dis.innerHTML;
	var array=list.split(";");
	var fields=array[mp_index].split("#");
	var old_price=fields[2];
	var amount=fields[1];
	array[mp_index]=fields[0]+"#"+fields[1]+"#"+price;
	list="";
	for(i=0;i<array.length-1;i++)
			list+=array[i]+";";
	box.value=list;
	
	total.innerHTML=total.innerHTML*1+amount*(price-old_price);
	array=dis_str.split("<br>");
	array[mp_index]=array[mp_index].replace("&nbsp;×&nbsp;"+amount+"&nbsp;=&nbsp;"+amount*old_price,
			"&nbsp;×&nbsp;"+amount+"&nbsp;=&nbsp;"+amount*price);
	dis_str="";
	for(i=0;i<array.length-1;i++)
			dis_str+=array[i]+"<br>";
	dis.innerHTML=dis_str;
}